package com.student.education.controller;

import com.student.education.common.CommonPage;
import com.student.education.common.CommonResult;
import com.student.education.entity.Clazz;
import com.student.education.entity.User;
import com.student.education.entity.UserDownload;
import com.student.education.service.UserService;
import com.student.education.to.ExamScoreTo;
import com.student.education.to.UserTo;
import com.student.education.util.ExcelUtils;
import com.student.education.vo.MyStudentVo;
import com.student.education.vo.UserQueryVo;
import com.student.education.vo.UserVo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 用户表(User)表控制层
 *
 * @author makejava
 * @since 2021-09-14 16:25:52
 */
@RestController
@RequestMapping("/user")
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;

    @Value("${server.port}")
    private String port;

    @PostMapping("/login")
    public CommonResult<UserTo> selectOne(@RequestBody UserVo vo) {
        UserTo userTo = userService.queryByLogin(vo);
        if (!Objects.equals(userTo,null)){
            return CommonResult.success(userTo);
        }
        return CommonResult.error("用户名/密码错误");
    }

    @RequestMapping(value="/loginOut" , method = RequestMethod.GET)
    public void loginOut(HttpServletRequest request, HttpServletResponse response) throws Exception {
        response.sendRedirect("http://localhost:"+port);
    }

    /**
     *
     * 通过主键查询单条数据
     *
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("/selectMyInfo")
    public CommonResult<UserTo> selectMyInfo(Integer userId) {
        return CommonResult.success(userService.queryById(userId));
    }
    /**
     * 用户自我信息修改
     *
     * @param vo 主键
     * @return 单条数据
     */
    @PostMapping("/updateMyInfo")
    public CommonResult<UserTo> updateMyInfo(@RequestBody User vo) {
        int result = userService.updateMyInfo(vo);
        if (Objects.equals(result,1)){
            return CommonResult.success("修改成功");
        }
        return CommonResult.error("修改失败");
    }

    @PostMapping("/queryAll")
    public CommonResult<CommonPage<UserTo>> queryAll(@RequestBody UserQueryVo vo) {
        return CommonResult.success(userService.queryAll(vo));
    }


    @PostMapping("/register")
    public CommonResult<String> register(@RequestBody User vo) {
        int result = userService.insert(vo);
        if (Objects.equals(result,1)){
            return CommonResult.success("新增成功");
        }
        return CommonResult.error("新增失败");
    }



    @PostMapping("/delete")
    public CommonResult<String> delete(@RequestBody List<Integer> ids) {
        int result = userService.delete(ids);
        if (result>0){
            return CommonResult.success("删除成功");
        }
        return CommonResult.error("删除失败");
    }

    @PostMapping("/update")
    public CommonResult<UserTo> update(@RequestBody User vo) {
        int result = userService.updateMyInfo(vo);
        if (Objects.equals(result,1)){
            return CommonResult.success("修改成功");
        }
        return CommonResult.error("修改失败");
    }

    @GetMapping("/queryMyClass")
    public CommonResult<List<Clazz>> queryMyClass(Integer userId) {
        List<Clazz> list = userService.queryMyClass(userId);
        return CommonResult.success(list);
    }
    @PostMapping("/queryMyStudents")
    public CommonResult<CommonPage<UserTo>> queryMyStudents(@RequestBody MyStudentVo vo) {
        return CommonResult.success(userService.queryMyStudents(vo));
    }

    @PostMapping("/addMyStudents")
    public CommonResult<String> addMyStudents(@RequestBody User vo) {
        vo.setPassword("123456");vo.setRoleId("3");
        int result = userService.insert(vo);
        if (Objects.equals(result,1)){
            return CommonResult.success("新增成功");
        }
        return CommonResult.error("新增失败");
    }



    /**
     * 学生信息模板下载
     * @param response
     */
    @GetMapping("/downloadStudentsModel")
    public void downloadModel(HttpServletRequest request, HttpServletResponse response){
        List<UserDownload> empList = new ArrayList<>();
        ExcelUtils.writeExcel(request,response, empList, UserDownload.class);
        return;
    }
    /**
     * Excel上传学生信息
     * @param file
     */
    @PostMapping("/uploadStudentsModel")
    public CommonResult<String> readExcel(@RequestBody MultipartFile file){
        System.out.println("上传的文件："+file.getOriginalFilename());
        List<UserDownload> list = ExcelUtils.readExcel("", UserDownload.class, file);
        int result = userService.insertDD(list);
        return CommonResult.success("上传完成：失败"+result+"条");
    }


}